layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 发票管理
     */
    var FlowBill = {
        tableId: "flowBillTable"
    };

    /**
     * 初始化表格的列
     */
    FlowBill.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'billId', hide: true, title: 'ID'},
            {field: 'orderCode', sort: true, title: '订单编号'},
            {field: 'status', sort: true, title: '状态',templet:function (row) {
                    if(row.status == 0){
                        return "待处理";
                    } else if(row.status == 1){
                        return "处理中";
                    } else if(row.status == 2){
                        return "已开票";
                    } else {
                        return "此单作废";
                    }
                }},
            {field: 'titleType', sort: true, title: '抬头类型',templet:function (row) {
                if(row.titleType == 0){
                    return "企业/单位";
                }else {
                    return "个人/非企业单位";
                }
            }},

            {field: 'title', sort: true, title: '抬头'},
            {field: 'idCode', sort: true, title: '纳税号'},
            {field: 'note', sort: true, title: '备注'},
            {field: 'moneys', sort: true, title: '发票金额'},
            {field: 'billType', sort: true, title: '发票类型',templet:function (row) {
                    if(row.billType == 0){
                        return "纸质发票";
                    }else{
                        return "电子发票";
                    }
                }},
            {field: 'createTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FlowBill.search = function () {
        var queryData = {};

        queryData['orderCode'] = $('#orderCode').val();
        queryData['contact'] = $('#contact').val();
        queryData['phone'] = $('#phone').val();

        table.reload(FlowBill.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    FlowBill.openAddDlg = function () {
        func.open({
            title: '添加发票',
            content: Feng.ctxPath + '/flowBill/add',
            tableId: FlowBill.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      FlowBill.openEditDlg = function (data) {
          func.open({
              title: '修改发票',
              content: Feng.ctxPath + '/flowBill/edit?billId=' + data.billId,
              tableId: FlowBill.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    FlowBill.exportExcel = function () {
        var checkRows = table.checkStatus(FlowBill.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowBill.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowBill/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(FlowBill.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("billId", data.billId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FlowBill.tableId,
        url: Feng.ctxPath + '/flowBill/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FlowBill.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FlowBill.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    FlowBill.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        FlowBill.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + FlowBill.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FlowBill.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FlowBill.onDeleteItem(data);
        }
    });
});
